home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
usenet
/
sources
/
volume91
/
news
/
barn_201
/
part01
/
configure.c
< prev
next >
Wrap
Text File
|
1991-02-07
|
5KB
|
182 lines
/*
* File Name: configure.c
* Project: BARN - Bah's Amiga ReadNews.
* Purpose: Configuration variable setting, retrieving.
* Functions: Configure, SetVar, GetVar.
* Author: Jeff Van Epps
* Created: 20 Oct 90
* Last Modified: 20 Oct 90
* Comments:
* Generic configuration file processor. Reads lines of the form:
* var=value
* from a specified configuration file and stores the names and values.
* White space WILL be considered significant, so don't put any in there
* unless you really want it. No '=' may appear anywhere else on the line.
*
* SetVar(name,value) changes an existing value or creates a new one.
* GetVar(name) returns a pointer to the value of a variable.
* Configure(filename) processes a configuration file.
*
* A line in the configuration file beginning with '#' is a comment.
* Comment lines and blank lines are skipped.
*
* Currently implemented with static limits for name and value length as
* well as number of possible variables.
*
* History:
* 20 Oct 90/JVE Created.
*/
# include <stdio.h>
# include <string.h>
# include "standard.h"
# include "configure.h"
# define MAX_VARS 20
# define MAX_NAME_LEN 10
# define MAX_VALUE_LEN 40
static struct {
char name[MAX_NAME_LEN+1]; /* name of variable */
char value[MAX_VALUE_LEN+1]; /* its value */
} vars[MAX_VARS];
static int n_vars = 0;
/****************************************************************************/
/* FUNCTION: Configure */
/* */
/* PURPOSE: Parse ARN configuration file. */
/* */
/* INPUT PARAMETERS: */
/* NAME I/O DESCRIPTION */
/* ---- --- ----------- */
/* config_file I Name of configuration file to use. */
/* */
/* RETURNS: */
/* */
/* COMMENTS: */
/* */
/* HISTORY: */
/* 1. 20 Oct 90 Created. */
/* */
/****************************************************************************/
void Configure( config_file )
char *config_file;
{
FILE *fp;
char s[MAXLINE], *name, *value;
if ( ( fp = fopen( config_file, "r" ) ) == NULL )
{
perror( config_file );
exit( 1 );
}
while ( fgets( s, MAXLINE, fp ) != NULL )
{
s[strlen(s)-1] = NULL; /* kill trailing newline */
if ( s[0] != '\0' && s[0] != '#' ) /* skip blank and comment lines */
{
if ( ( name = strtok( s, "=" ) ) == NULL )
fprintf( stderr, "Can't parse '%s'\n", s );
else
{
value = strtok( NULL, "=" );
(void) SetVar( name, value );
}
}
}
fclose( fp );
}
/****************************************************************************/
/* FUNCTION: SetVar */
/* */
/* PURPOSE: Set the value of a configuration variable. */
/* */
/* INPUT PARAMETERS: */
/* NAME I/O DESCRIPTION */
/* ---- --- ----------- */
/* name I Variable name. */
/* value I Variable value. */
/* */
/* RETURNS: */
/* OK Successful. */
/* ERROR Unsuccessful. */
/* */
/* COMMENTS: */
/* */
/* HISTORY: */
/* 1. 20 Oct 90 Created. */
/* */
/****************************************************************************/
SetVar( name, value )
char *name;
char *value;
{
int i;
if ( strlen( name ) > MAX_NAME_LEN )
name[MAX_NAME_LEN-1] = NULL;
if ( strlen( value ) > MAX_VALUE_LEN )
value[MAX_VALUE_LEN-1] = NULL;
for ( i = 0; i < n_vars; i++ )
if ( strcmp( vars[i].name, name ) == 0 )
{
strcpy( vars[i].value, value );
return OK;
}
if ( n_vars >= MAX_VARS )
{
fprintf( stderr, "Too many variables!\n" );
return ERROR;
}
strcpy( vars[i].name, name );
strcpy( vars[i].value, value );
n_vars++;
return OK;
}
/****************************************************************************/
/* FUNCTION: GetVar */
/* */
/* PURPOSE: Return value of configuration variable. */
/* */
/* INPUT PARAMETERS: */
/* NAME I/O DESCRIPTION */
/* ---- --- ----------- */
/* name I Name of variable. */
/* */
/* RETURNS: */
/* (char *) Pointer to value. */
/* (char *) NULL No such variable known. */
/* */
/* COMMENTS: */
/* */
/* HISTORY: */
/* 1. 20 Oct 90 Created. */
/* */
/****************************************************************************/
char *GetVar( name )
char *name;
{
int i;
for ( i = 0; i < n_vars; i++ )
if ( strcmp( vars[i].name, name ) == 0 )
return vars[i].value;
return NULLP( char );
}